<!--  -->
<template>
  <div class="base-echarts">
    <div ref="echartsDivRef" :style="{ width: width, height: height }"></div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, defineProps, withDefaults, watchEffect } from 'vue'
import * as echarts from 'echarts'
import chinaMapData from '../data/china.json'
echarts.registerMap('china', chinaMapData)
import { EChartsOption } from 'echarts'
const props = withDefaults(
  // 定义props
  defineProps<{
    width?: string
    height?: string
    options: EChartsOption
  }>(),
  // 默认值
  {
    width: '100%',
    height: '360px'
  }
)

const echartsDivRef = ref<HTMLElement>()
onMounted(() => {
  const echartsInstance = echarts.init(echartsDivRef.value!)
  // 当数据发生改变时重新渲染图表
  watchEffect(() => {
    echartsInstance.setOption(props.options)
  })

  // 图标页面大小适配
  window.addEventListener('resize', () => {
    echartsInstance.resize()
  })
})
</script>
<style lang="less" scoped></style>
